package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.mapper.SysOrgMapper;
import com.zimi.mapper.SysUserMapper;
import com.zimi.model.SysOrg;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysUser;
import com.zimi.service.SysOrgService;
import com.zimi.util.CommonUtil;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统组织机构 api
 *
 * @author lnintr
 * @since 2019-05-20
 */
@Api
@RestController
@RequestMapping("sys-org")
public class SysOrgApi {

    @Autowired
    private SysOrgService service;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;

    @Autowired
    private CommonUtil commonService;


    @PostMapping("recycleList")
    @ApiOperation("查询机构回收站信息")
    public Object selectRecycleList(SysOrgExtend record, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<SysOrgExtend> list = service.selectRecycleList();
        PageInfo<SysOrgExtend> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    @PostMapping("recycle")
    @ApiOperation("机构回收")
    public Object recycle(HttpServletRequest request) {
        String[] ma = request.getParameterValues("orgList");

        List<SysOrgExtend> sysOrgList = GsonUtil.json2Objs(ma[0], SysOrgExtend.class);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);

        if (sysOrgList != null && sysOrgList.size() > 0) {
            map.put("confirmFlag", service.recycle(sysOrgList));
        }

        return map;
    }

    @PostMapping("recycleConfirm")
    @ApiOperation("机构回收")
    public Object recycleConfirm() {
        service.recycleConfirm();

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    @PostMapping("getCompanyByCateIds")
    @ApiOperation("查询机构信息")
    public Object getCompanyByCateIds(SysOrg record, int[] arr) {
        List<SysOrg> list = service.getCompanyByCateIds(record, arr);

        Map<String, Object> objectMap = new HashMap<>();

        objectMap.put("data", list);
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("orgList")
    @ApiOperation("查询机构信息")
    public Object selectOrgList(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", service.getOrglistByOperator(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("getOrgList")
    @ApiOperation("查询关联机构信息")
    public Object getOrgList() {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", commonService.getOrgList());
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("orgListByCate")
    @ApiOperation("查询机构信息")
    public Object selectOrgListByCate(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", service.getOrgList(record.getParentId() == null ? 0 : record.getParentId()));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("allOrgAndUser")
    @ApiOperation("查询机构和用户信息")
    public Object allOrgAndUser(SysOrgExtend record) {
        Map<String, Object> objectMap = new HashMap<>();
        //取所有机构
        List<SysOrg> allOrg = sysOrgMapper.selectList(new QueryWrapper<SysOrg>().lambda().eq(SysOrg::getOrgState, 1).orderByAsc(SysOrg::getOrgSort));

        //取所有用户
        List<SysUser> allUser = new SysUser().selectAll();

        objectMap.put("orgList", allOrg);
        objectMap.put("userList", allUser);
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("add")
    @ApiOperation("新建机构")
    public Object add(SysOrg sysOrg) {
        if (sysOrg.getOrgPid() == null) {
            sysOrg.setOrgPid(0);
        }

        service.insert(sysOrg);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("changeDep")
    @ApiOperation("切换上级部门")
    public Object changeDep(SysOrg sysOrg) {

        service.changeDep(sysOrg);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("update")
    @ApiOperation("修改机构")
    public Object upd(SysOrg sysOrg) {
        service.update(sysOrg);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("delete")
    @ApiOperation("删除机构")
    public Object del(HttpServletRequest request) {
        String[] ma = request.getParameterValues("orgList");
        List<SysOrg> sysOrgList = GsonUtil.json2Objs(ma[0], SysOrg.class);

        service.delete(sysOrgList);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("move")
    @ApiOperation("移动机构树")
    public Object move(HttpServletRequest request) {
        String[] ma = request.getParameterValues("idList");
        List<SysOrg> sysOrgList = GsonUtil.json2Objs(ma[0], SysOrg.class);

        service.move(sysOrgList);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }


    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public Object getUserById(@PathVariable("id") int id) {
        Map map = new HashMap();
        SysOrg sysOrg = new SysOrg();
        SysOrg org = sysOrg.selectById(id);
        map.put("code", 0);
        map.put("sysOrg", org);

        return map;
    }

    @PostMapping("getInfoById")
    @ApiOperation("根据id获取信息")
    public Object getInfoById(SysOrg org) {
        Map map = new HashMap();
        SysOrg sysOrg = org.selectById();

        map.put("code", 0);
        map.put("data", sysOrg);

        return map;
    }

    @PostMapping("getOrgTreeById")
    @ApiOperation("根据id获取信息")
    public Object getOrgTreeById(SysOrg org) {
        Map map = new HashMap();
        List<SysOrgExtend> list = service.getOrgTreeById(org.getId());

        map.put("code", 0);
        map.put("data", list);

        return map;
    }


}
